package com.twu.biblioteca;

import java.util.*;
/**
 * Created with IntelliJ IDEA.
 * User: kush jain
 * Date: 6/28/13
 * Time: 5:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class Menu {
    ArrayList menuOptions;
    int choiceCount;
    Menu()
    {
        menuOptions = new ArrayList();
    }

    public void setMenuOptions()
    {
        int index = 1;

        if(Session.curuser!=null)
        {
            index = customerMenu(index);
            index = authenticatedUserMenu(index);
        }
        else
        {
            if(!Session.isAdmin)
                index = guestMenu(index);
            else
            {
                index = adminMenu(index);
                index = authenticatedUserMenu(index);
            }
        }
        index = publicMenu(index);
        choiceCount = index-1;
    }

    private int authenticatedUserMenu(int index) {
        menuOptions.add(new MenuChoice(index++,"Logout"));
        return index;
    }

    private int guestMenu(int index) {
        menuOptions.add(new MenuChoice(index++,"Login as Member"));
        menuOptions.add(new MenuChoice(index++,"Login as Administrator"));
        return index;
    }

    private int customerMenu(int index) {
        menuOptions.add(new MenuChoice(index++,"Login as Administrator"));
        menuOptions.add(new MenuChoice(index++,"Reserve Book"));
        return index;
    }

    private int publicMenu(int index) {
        menuOptions.add(new MenuChoice(index++,"Show All Books"));
        menuOptions.add(new MenuChoice(index++,"Search Book by Title"));
        menuOptions.add(new MenuChoice(index++,"Show Reserved Books"));
        menuOptions.add(new MenuChoice(index++,"Show List of Movies"));
        menuOptions.add(new MenuChoice(index++,"Check Membership Status"));
        menuOptions.add(new MenuChoice(index++,"Exit"));
        return index;
    }

    private int adminMenu(int index) {
        menuOptions.add(new MenuChoice(index++,"Add Book"));
        menuOptions.add(new MenuChoice(index++,"Remove Book"));
        menuOptions.add(new MenuChoice(index++,"Free Reserved Book"));
        menuOptions.add(new MenuChoice(index++,"Add Movie"));
        menuOptions.add(new MenuChoice(index++,"Add User"));
        menuOptions.add(new MenuChoice(index++,"Remove User"));
        menuOptions.add(new MenuChoice(index++,"Login as Member"));
        return index;
    }

    public String getMenuText()
    {
        String menuText = "";
        menuText = "\nMenu\n";
        for(int i=0;i<choiceCount;i++)
        {
          menuText = menuText + "\n\t" +  ((MenuChoice)menuOptions.get(i)).toString() ;
        }
        return menuText;
    }

    public String getChoice(String choice)
    {
            String errorString = "Select a Valid option!!";
            try
            {
                int choiceNo = Integer.parseInt(choice);
                if(choiceNo >choiceCount || choiceNo < 0)
                    throw new NotValidMenuOptionException();
                for(int i=0;i<choiceCount;i++)
                 {
                     if(((MenuChoice)menuOptions.get(i)).optionNoEquals(choiceNo))
                           return ((MenuChoice)menuOptions.get(i)).getLabel();

                 }

            }
            catch(Exception anyException)
            {
                  return errorString;
            }
          return errorString;
    }

}
